package com.example.front_back_separation_development.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class IDeviceServiceImpl implements IDeviceService {
    @Autowired
    DeviceRepository deviceRepository;
    @Override
    public Page<Device> findAllByPaged(Integer page,Integer size){
        Pageable request = PageRequest.of(page-1,size);
        Page<Device> all = deviceRepository.findAll(request);
        return all;
    }

    @Override
    public Page<Device> findByTitleLike(DeviceDTO deviceDTO) {
        Pageable request = PageRequest.of(deviceDTO.getPage()-1,deviceDTO.getSize());
        Page<Device> all = deviceRepository.findByTitleLike("%"+deviceRepository.getName()+"%",request);
        return all;
    }

    @Override
    public Boolean deleteById(DeviceDTO deviceDTO) {
        try {//包围方式的快捷键ctrl+alt+t
            deviceRepository.deleteById(deviceDTO.getId());
            return true;
        } catch (Exception e) {
           return false;
        }
    }

    @Override
    public Boolean addArticle(DeviceDTO deviceDTO) {
       Device device =  new Device(deviceDTO.getName(),deviceDTO.getType(),deviceDTO.getVersion());
        try {
            articleRepository.save(article);
            return true;
        } catch (Exception e) {
           e.printStackTrace();
           return false;
        }
    }

    @Override
    public Boolean updateArticle(ArticleDTO articleDTO) {
        Device device =  new Article(articleDTO.getId(),deviceDTO.getName(),deviceDTO.getType(),deviceDTO.getVersion());
        try {
            deviceRepository.save(device);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
